﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.抖音出入库.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.抖音出入库
{
    public partial class Form抖音下单修改商品信息 : DevExpress.XtraEditors.XtraForm
    {
        private DouyinOrderRequestDTO douyinOrderRequestDTO { get; set; }
        private DouyinOrderDTO douyinOrderDTO { get; set; }
        private DouyinMTSOrderDTO douyinMTSOrderDTO { get; set; }
        private String materialCode { get; set; }
        public Form抖音下单修改商品信息(DouyinOrderDTO douyinOrderDTO)
        {
            InitializeComponent();
            this.douyinOrderDTO = douyinOrderDTO;
            this.label款号.Text = "当前款号："+douyinOrderDTO.styleCode;
            this.label面料号.Text = "当前面料号：" + douyinOrderDTO.materialCode;
            this.label尺码.Text = "当前尺码：" + douyinOrderDTO.styleCode;
            this.textBox需求数量.Text = douyinOrderDTO.quantity.ToString();
            this.searchLookUpEdit面料.Properties.DataSource = MTO订单Service.getMaterialOrderIndexAll();
            //显示该款式的尺码组
            this.comboBox定制尺码.Items.Clear();
            List<String> 尺码 = new List<string>();
            尺码 = MTO订单Service.findSizeCdByStyleNameCn(douyinOrderDTO.styleCode);
            foreach (String str in 尺码)
            {
                this.comboBox定制尺码.Items.Add(str);
            }
            this.textBox抖音售价.Visible = false;
        }
        public Form抖音下单修改商品信息(DouyinMTSOrderDTO douyinMTSOrderDTO)
        {
            InitializeComponent();
            this.douyinMTSOrderDTO = douyinMTSOrderDTO;
            this.label款号.Text = "当前款号：" + douyinMTSOrderDTO.styleNameCn;
            this.label面料号.Text = "当前面料号：" + douyinMTSOrderDTO.materialCode;
            this.label尺码.Visible = false;
            this.searchLookUpEdit面料.Visible = false;
            //显示该款式的尺码组
            this.comboBox定制尺码.Items.Clear();
            List<String> 尺码 = new List<string>();
            尺码 = MTO订单Service.findSizeCdByStyleNameCn(douyinMTSOrderDTO.styleNameCn);
            foreach (String str in 尺码)
            {
                this.comboBox定制尺码.Items.Add(str);
            }
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            //
            if (String.IsNullOrEmpty(this.textBox需求数量.Text))
            {
                MessageBox.Show("请补充数量");
                return;
            }
            try
            {
                if (String.IsNullOrEmpty(this.douyinOrderDTO.styleCode))
                {
                    this.douyinMTSOrderDTO.price = Convert.ToInt32(this.textBox抖音售价.Text.ToString());
                    this.douyinMTSOrderDTO.styleSizeCd = this.comboBox定制尺码.Text;
                    this.douyinMTSOrderDTO.quantity = Convert.ToInt32(this.textBox需求数量.Text.ToString());
                }
                else
                {
                    this.douyinOrderDTO.styleSizeCd = this.comboBox定制尺码.Text;
                    this.douyinOrderDTO.quantity = Convert.ToInt32(this.textBox需求数量.Text.ToString());
                }
                
            }
            catch { }
            this.Close();
        }

        private void textBox需求数量_Validating(object sender, CancelEventArgs e)
        {
            if (!FunctionHelper.Is大于0(this.textBox需求数量.Text.Trim()))
            {
                MessageBox.Show("需求数量必须大于0！");
                this.ActiveControl = this.textBox需求数量;
                return;
            }
        }
        #region 选择面料
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit面料_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit面料View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit面料.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.materialCode = this.searchLookUpEdit面料.Properties.View.GetRowCellValue(rowHandle, "materialCode").ToString();
                this.douyinOrderDTO.materialCode = this.materialCode;
            }

            ////传materialCode到customerMadeSaleConfirmDTO
            //if (null == this.customerMadeSaleConfirmDTO)
            //{
            //    MessageBox.Show("请重新扫码");
            //    return;
            //}
            //this.textBox定制面料号.Text = this.materialCode;
            //this.textBox定制面料名称.Text = this.materialNameCn;
            //this.customerMadeSaleConfirmDTO.buildMaterialCode(materialCode);
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit面料.ToolTip = null;
            searchLookUpEdit面料.EditValue = null;
        }
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit面料_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.materialCode;
        }
        #endregion
    }
}
